package com.dai.spec.gxk.controller;

import com.dai.spec.common.annotation.Log;
import com.dai.spec.common.core.controller.BaseController;
import com.dai.spec.common.core.domain.AjaxResult;
import com.dai.spec.common.core.page.TableDataInfo;
import com.dai.spec.common.enums.*;
import com.dai.spec.common.utils.poi.ExcelUtil;
import com.dai.spec.gxk.domain.StudentCourse;
import com.dai.spec.gxk.request.MyStudentPageRequest;
import com.dai.spec.gxk.request.MyStudentScorePageRequest;
import com.dai.spec.gxk.service.MyStudentService;
import com.dai.spec.gxk.vo.MyStudentScoreVO;
import com.dai.spec.gxk.vo.MyStudentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 我的学生信息管理Controller
 * 老师接口，管理选了该老师教的课的学生
 * @author daikl
 * @date 2024-02-29
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/gxk/myStudent")
@Api("我的学生信息管理")
public class MyStudentController extends BaseController {

private MyStudentService myStudentService;

    public MyStudentController(MyStudentService myStudentService) {
        this.myStudentService = myStudentService;
    }

    /**
     * 查询我的学生列表
     */
    @ApiOperation("查询我的学生列表")
    @PreAuthorize("@ss.hasPermi('gxk:myStudent:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyStudentPageRequest pageRequest) {

      List<MyStudentVO> list = myStudentService.queryMyStudentList(pageRequest);
      return getDataTable(list);
    }
    /**
     * 查询我的学生成绩列表
     */
    @ApiOperation("查询我的学生成绩列表")
    @PreAuthorize("@ss.hasPermi('gxk:score:list')")
    @GetMapping("/score/list")
    public TableDataInfo list(MyStudentScorePageRequest pageRequest) {

        List<MyStudentScoreVO> list = myStudentService.queryMyStudentScoreList(pageRequest);
        return getDataTable(list);
    }
    /**
     * 获取学生成绩信详细信息
     */
    @ApiOperation("获取学生详细信息")
    @PreAuthorize("@ss.hasPermi('gxk:score:query')")
    @GetMapping(value = "/score/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(myStudentService.getById(id));
    }
    /**
     * 修改学生成绩，打分
     */
    @ApiOperation("打分")
    @PreAuthorize("@ss.hasPermi('gxk:score:edit')")
    @Log(title = "修改学生成绩", businessType = BusinessType.UPDATE)
    @PutMapping("/score")
    public AjaxResult edit(@RequestBody StudentCourse studentCourse) {
        return toAjax(myStudentService.updateScore(studentCourse));
    }

    /**
     * 导出我的学生信息管理列表
     */
    @ApiOperation("导出我的学生列表")
    @PreAuthorize("@ss.hasPermi('gxk:myStudent:export')")
    @Log(title = "我的学生信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MyStudentPageRequest pageRequest) {

        List<MyStudentVO> myStudentVOList = myStudentService.queryMyStudentList(pageRequest);
        List<MyStudentVO> studentVOList = myStudentVOList.stream().map(item -> {
            item.setSex(SexType.getValueByCode(item.getSex()));
            item.setDeliveryType(DeliveryType.getValueByCode(item.getDeliveryType()));
            return item;
        }).collect(Collectors.toList());
        ExcelUtil<MyStudentVO> util = new ExcelUtil<>(MyStudentVO.class);
        util.exportExcel(response, studentVOList, "学生信息管理数据");
    }

    /**
     * 导出我的学生成绩信息管理列表
     */
    @ApiOperation("导出我的学生成绩列表")
    @PreAuthorize("@ss.hasPermi('gxk:myStudentScore:export')")
    @Log(title = "我的学生成绩信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/score/export")
    public void exportCourse(HttpServletResponse response, MyStudentScorePageRequest pageRequest) {

        List<MyStudentScoreVO> myStudentScoreVOS = myStudentService.queryMyStudentScoreList(pageRequest);
        List<MyStudentScoreVO> studentScoreVOList = myStudentScoreVOS.stream().map(item -> {
            item.setDeliveryType(DeliveryType.getValueByCode(item.getDeliveryType()));
            item.setCourseCycle(CourseCycleType.getValueByCode(item.getCourseCycle()));
            item.setType(CourseType.getValueByCode(item.getType()));
            return item;
        }).collect(Collectors.toList());
        ExcelUtil<MyStudentScoreVO> util = new ExcelUtil<>(MyStudentScoreVO.class);
        util.exportExcel(response, studentScoreVOList, "学生成绩信息管理数据");
    }

}
